import { ref } from 'vue';
import { defineStore } from 'pinia';
import { OrderApi, OrderTimelineNode } from '@/api/ordermange/index';

export const useOrderStore = defineStore('order', () => {
  // 使用 ref<Type>() 来为 ref 提供类型
  const timelineNodes = ref<OrderTimelineNode[]>([]);
  const isLoading = ref<boolean>(false);
  const error = ref<string | null>(null);

  async function fetchTimeline(orderId: number): Promise<void> {
    isLoading.value = true;
    error.value = null;
    try {
      // getOrderTimeline 的返回值已经是类型安全的 OrderTimelineNode[]
      timelineNodes.value = await OrderApi.getOrderTimeline(orderId);
    } catch (err) {
      
      if (err instanceof Error) {
        error.value = err.message;
      } else {
        error.value = '获取订单时间轴失败';
      }
      console.error("Failed to fetch order timeline:", err);
    } finally {
      isLoading.value = false;
    }
  }

  return { 
    timelineNodes,
    isLoading,
    error,
    fetchTimeline 
  };
});
